package service.impl;

import java.util.List;
import java.util.Map;

import com.opensymphony.xwork2.ActionContext;

import model.EstpInfo;
import model.Experience;
import model.Knowledge;
import model.User;
import dao.KnowledgeDao;
import service.KnowledgeManager;

public class KnowledgeManagerImpl implements KnowledgeManager {
	private KnowledgeDao knowledgeDao;
	
	public KnowledgeDao getKnowledgeDao() {
		return knowledgeDao;
	}

	public void setKnowledgeDao(KnowledgeDao knowledgeDao) {
		this.knowledgeDao = knowledgeDao;
	}

	public int addKnowledgeManager(int ledgeSort, String klName,
			int continent, int country, String ledgeNumber)
			throws Exception {
		try {
			Knowledge k = new Knowledge();
			k.setLedgeSort(ledgeSort);
			k.setKlName(klName);
			k.setContinent(continent);
			k.setCountry(country);
			k.setLedgeNumber(ledgeNumber);
			Map session = ActionContext.getContext().getSession();
			User user = (User)session.get("USER");
			k.setUser(user);
			knowledgeDao.save(k);
			return k.getId();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			throw new Exception("新增知识产权信息时出现异常");
		}
		// TODO Auto-generated method stub
	}
	
//	public int addOrUpdateKnowledgeManager(int ledgeSort, String klName,
//			int continent, int country, String ledgeNumber)
//			throws Exception {
//		try {
//			Map session = ActionContext.getContext().getSession();
//			Knowledge k = (Knowledge)session.get("K");
//			if(k == null){
//				k = new Knowledge();
//			}
//			k.setLedgeSort(ledgeSort);
//			k.setKlName(klName);
//			k.setContinent(continent);
//			k.setCountry(country);
//			k.setLedgeNumber(ledgeNumber);
//			User user = (User)session.get("USER");
//			k.setUser(user);
//			knowledgeDao.saveOrUpdate(k);
//			return k.getId();
//		} catch (Exception e) {
//			// TODO: handle exception
//			e.printStackTrace();
//			throw new Exception("新增知识产权信息时出现异常");
//		}
//		// TODO Auto-generated method stub
//	}
//	
	public List<Knowledge> getKnowledgeByUser(User user) throws Exception{
		try {
			List<Knowledge> kInfo = knowledgeDao.getKnowledgeByUser(user);
			return kInfo;
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			throw new Exception("按用户查找知识产权信息时出现异常");
		}
	}
	public void delAllInfo() throws Exception{
		try {
			Map session = ActionContext.getContext().getSession();
			User user = (User)session.get("USER");
			knowledgeDao.delete(user);
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			throw new Exception("删除知识产权信息时出现异常");
		}
	}
}
